package request

import (
	"time"
	// 可能需要导入 model 包，如果 DateOnly 定义在 model 包中
	"wms/internal/model" // 导入 model 包
)

// InventoryListRequest 库存列表请求
type InventoryListRequest struct {
	PageRequest
	Keyword     string `form:"keyword"`      // 关键字(货物编码、名称)
	WarehouseID uint   `form:"warehouse_id"` // 仓库ID
	LocationID  uint   `form:"location_id"`  // 货位ID
	CargoID     *uint  `form:"cargo_id"`     // 货物ID
	CargoCode   string `form:"cargo_code"`   // 货物编码
	BatchNo     string `form:"batch_no"`     // 批次号
	Status      *int   `form:"status"`       // 状态
	SortBy      string `form:"sort_by"`      // 排序字段
	SortOrder   string `form:"sort_order"`   // 排序方式
}

// InventoryAdjustRequest 库存调整请求
type InventoryAdjustRequest struct {
	InventoryID    uint    `json:"inventory_id"`                        // 库存ID
	CargoID        uint    `json:"cargo_id" binding:"required"`         // 货物ID
	LocationID     uint    `json:"location_id" binding:"required,gt=0"` // 库位ID
	AdjustQuantity float64 `json:"adjust_quantity" binding:"required"`  // 调整数量（正数增加，负数减少）
	Remark         string  `json:"remark" binding:"required,max=500"`   // 调整原因/备注
}

// BaseDocumentRequest 单据基础请求
type BaseDocumentRequest struct {
	WarehouseID uint   `json:"warehouse_id" binding:"required"` // 仓库ID
	Notes       string `json:"notes"`                           // 备注
}

// BaseDocumentListRequest 单据列表基础请求
type BaseDocumentListRequest struct {
	PageRequest
	Keyword     string `form:"keyword"`      // 关键字(单据号)
	WarehouseID uint   `form:"warehouse_id"` // 仓库ID
	Status      int    `form:"status"`       // 状态：1-待审核 2-已审核
	StartTime   string `form:"start_time"`   // 开始时间
	EndTime     string `form:"end_time"`     // 结束时间
}

// DocumentItem 单据货物项 (Renamed from DocumentItem for clarity)
type DocumentItem struct {
	CargoID   uint    `json:"cargo_id" binding:"required"`   // 货物ID
	Quantity  float64 `json:"quantity" binding:"required"`   // 数量
	UnitPrice float64 `json:"unit_price" binding:"required"` // 单价 (Renamed from Price for clarity)
	Note      string  `json:"note"`                          // 备注
}

// InboundCreateRequest 创建入库单请求
type InboundCreateRequest struct {
	BaseDocumentRequest
	Type         int            `json:"type" binding:"required"`       // 入库类型（1:采购入库 2:退货入库 3:其他入库）
	SupplierID   *uint          `json:"supplier_id"`                   // 供应商ID
	ExpectedTime *time.Time     `json:"expected_time"`                 // 预计到货时间 (Add this field)
	OrderNo      string         `json:"order_no"`                      // 关联单号 (Added)
	Items        []DocumentItem `json:"items" binding:"required,dive"` // 入库货物列表
}

// InboundUpdateRequest 更新入库单请求
type InboundUpdateRequest struct {
	BaseDocumentRequest
	Type        int            `json:"type" binding:"required"`         // 入库类型 (Re-added)
	WarehouseID uint           `json:"warehouse_id" binding:"required"` // 仓库ID (Added for clarity)
	SupplierID  *uint          `json:"supplier_id"`                     // 供应商ID (Added)
	OrderNo     string         `json:"order_no"`                        // 关联单号 (Added)
	Note        string         `json:"note"`                            // 备注 (Added)
	Items       []DocumentItem `json:"items" binding:"required,dive"`   // 入库货物列表
}

// InboundListRequest 入库单列表请求
type InboundListRequest struct {
	BaseDocumentListRequest
}

// OutboundCreateRequest 创建出库单请求
type OutboundCreateRequest struct {
	BaseDocumentRequest
	Type       int            `json:"type" binding:"required"`       // 出库类型（1:销售出库 2:退货出库 3:其他出库）
	CustomerID *uint          `json:"customer_id"`                   // 客户ID
	OrderNo    string         `json:"order_no"`                      // 关联单号 (Added)
	Items      []DocumentItem `json:"items" binding:"required,dive"` // 出库货物列表
}

// OutboundUpdateRequest 更新出库单请求
type OutboundUpdateRequest struct {
	BaseDocumentRequest
	Type        int            `json:"type" binding:"required"`         // 出库类型 (Re-added)
	WarehouseID uint           `json:"warehouse_id" binding:"required"` // 仓库ID (Added for clarity)
	CustomerID  *uint          `json:"customer_id"`                     // 客户ID (Added)
	OrderNo     string         `json:"order_no"`                        // 关联单号 (Added)
	Note        string         `json:"note"`                            // 备注 (Added)
	Items       []DocumentItem `json:"items" binding:"required,dive"`   // 出库货物列表
}

// OutboundListRequest 出库单列表请求
type OutboundListRequest struct {
	BaseDocumentListRequest
}

// ListInventoryAlertRequest 库存预警列表请求
type ListInventoryAlertRequest struct {
	PageRequest
	Keyword     string `form:"keyword"`      // 关键字(货物编码、名称)
	WarehouseID *uint  `form:"warehouse_id"` // 仓库ID
	AlertType   int    `form:"alert_type"`   // 预警类型：1-库存不足 2-库存积压
}

// PageInfo 分页请求基础结构
type PageInfo struct {
	Page     int `json:"page" form:"page"`           // 页码
	PageSize int `json:"page_size" form:"page_size"` // 每页数量
}

// GetPage 获取页码
func (p *PageInfo) GetPage() int {
	if p.Page <= 0 {
		p.Page = 1
	}
	return p.Page
}

// GetPageSize 获取每页数量
func (p *PageInfo) GetPageSize() int {
	if p.PageSize <= 0 {
		p.PageSize = 10
	}
	if p.PageSize > 100 {
		p.PageSize = 100
	}
	return p.PageSize
}

// InventoryHistoryRequest 库存历史记录请求
type InventoryHistoryRequest struct {
	PageInfo
	CargoID     uint   `json:"cargo_id" form:"cargo_id"`         // 货物ID (Made optional for filtering)
	WarehouseID uint   `json:"warehouse_id" form:"warehouse_id"` // 仓库ID (Added filter)
	LocationID  uint   `json:"location_id" form:"location_id"`   // 货位ID (Added filter)
	Type        string `json:"type" form:"type"`                 // 变动类型 (Added filter)
	StartDate   string `json:"start_date" form:"start_date"`     // 开始日期 (Added filter)
	EndDate     string `json:"end_date" form:"end_date"`         // 结束日期 (Added filter)
}

// InventoryStatusRequest 库存状态请求
type InventoryStatusRequest struct {
	CargoID uint `json:"cargo_id" form:"cargo_id" binding:"required"` // 货物ID
}

// InventoryAlertRequest 库存预警设置请求 (Updating Cargo Min/Max stock)
type InventoryAlertRequest struct {
	CargoID  uint     `json:"cargo_id" binding:"required"` // 货物ID
	MinStock *float64 `json:"min_stock"`                   // 最小库存阈值 (Pointer to allow nil)
	MaxStock *float64 `json:"max_stock"`                   // 最大库存阈值 (Pointer to allow nil)
}

// InventoryLocationRequest 库位库存查询请求
type InventoryLocationRequest struct {
	PageInfo
	CargoName string `json:"cargo_name" form:"cargo_name"` // 货物名称
	CargoCode string `json:"cargo_code" form:"cargo_code"` // 货物编码
	SortBy    string `json:"sort_by" form:"sort_by"`       // 排序字段
	SortOrder string `json:"sort_order" form:"sort_order"` // 排序方向：asc-升序，desc-降序
	BatchNo   string `json:"batch_no" form:"batch_no"`     // 批次号
}

// --- Inbound Operation Requests ---

// ReceiveItem represents a single item being received.
type ReceiveItem struct {
	OrderItemID      uint            `json:"order_item_id" binding:"required"`           // 入库单明细ID
	ReceivedQuantity float64         `json:"received_quantity" binding:"required,gte=0"` // 实收数量
	BatchNo          string          `json:"batch_no"`                                   // 批次号
	ProductionDate   *model.DateOnly `json:"production_date"`                            // 生产日期 (使用 model.DateOnly)
	ExpiryDate       *model.DateOnly `json:"expiry_date"`                                // 有效期至 (使用 model.DateOnly)
	// Add other fields if needed, like quality status, remark, etc.
}

// ReceiveRequest represents the request body for receiving inbound order items.
type ReceiveRequest struct {
	Items          []ReceiveItem `json:"items" binding:"required,dive"` // 使用 dive 验证嵌套结构体
	Note           string        `json:"note"`
	PreserveStatus bool          `json:"preserveStatus"` // 保持入库单状态不变，true表示不自动更新为待上架状态
}

// PutawayItem represents a single item being put away.
type PutawayItem struct {
	OrderItemID     uint    `json:"order_item_id" binding:"required"`         // 入库单明细ID
	LocationID      uint    `json:"location_id" binding:"required,gt=0"`      // 上架库位ID
	PutawayQuantity float64 `json:"putaway_quantity" binding:"required,gt=0"` // 本次上架数量
}

// PutawayRequest represents the request body for putting away inbound order items.
type PutawayRequest struct {
	Items []PutawayItem `json:"items" binding:"required,dive"`
	Note  string        `json:"note"`
}

// --- Outbound Operation Requests ---

// PickItem represents a single item being picked from a specific location.
type PickItem struct {
	OrderItemID    uint    `json:"order_item_id" binding:"required"`        // 出库单明细ID
	LocationID     uint    `json:"location_id" binding:"required,gt=0"`     // 拣货库位ID
	PickedQuantity float64 `json:"picked_quantity" binding:"required,gt=0"` // 本次拣货数量
	// BatchNo might be confirmed during picking
	BatchNo string `json:"batch_no"` // 确认的批次号 (可选)
}

// PickRequest represents the request body for picking outbound order items.
type PickRequest struct {
	Items []PickItem `json:"items" binding:"required,dive"`
	Note  string     `json:"note"`
}

// ShipItem represents a single item being shipped.
type ShipItem struct {
	OrderItemID     uint    `json:"order_item_id" binding:"required"`         // 出库单明细ID
	ShippedQuantity float64 `json:"shipped_quantity" binding:"required,gt=0"` // 本次发货数量 (通常等于已拣未发)
}

// ShipRequest represents the request body for shipping an outbound order.
type ShipRequest struct {
	TrackingNumber string     `json:"tracking_number"` // 使用下划线格式
	Carrier        string     `json:"carrier"`
	ShippingCost   *float64   `json:"shipping_cost"` // 运费，可选，使用下划线格式
	ShippedTime    *time.Time `json:"shipped_time"`  // 发货时间，可选，默认当前时间，使用下划线格式
	Items          []ShipItem `json:"items" binding:"required,dive"`
	Note           string     `json:"note"`
}

// CancelRequest struct for cancellation of orders.
type CancelRequest struct {
	Reason string `json:"reason"` // 移除 binding:"required"
}

// UpdateStatusRequest represents a request to update order status.
type UpdateStatusRequest struct {
	Status string `json:"status" binding:"required"` // 新状态值 (completed, cancelled, etc.)
}

// ProductListRequest 商品列表查询请求
type ProductListRequest struct {
	PageRequest
	Keyword    string `form:"keyword"`     // 商品关键字(编码/名称)
	CategoryID uint   `form:"category_id"` // 商品分类ID
	Status     int    `form:"status"`      // 商品状态
}
